package dev.pda.display.presentation.page.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.google.android.exoplayer2.ExoPlaybackException
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
import com.google.android.exoplayer2.ui.PlayerView
import dev.pda.display.R
import dev.pda.display.presentation.page.widgets.video.ExoPlayerManger


class VideoView : LinearLayout {
    private var view: View? = null
    private var simpleExoPlayerView: PlayerView? = null

    constructor(context: Context?) : super(context) {
        init(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    @SuppressLint("CustomViewStyleable")
    private fun init(attrs: AttributeSet?) {
        view = LayoutInflater.from(context).inflate(R.layout.view_video_view, this)
        simpleExoPlayerView = view?.findViewById(R.id.ep)
    }

    fun initVideoView(videoPath:String) {
        val exoPlayerManger = ExoPlayerManger()
        exoPlayerManger.setBuilderContext(context)
        exoPlayerManger.setVideoUrl(videoPath)
        val simpleExoPlayer = exoPlayerManger.create()
        //设置音量  测试期间设置为0
        //设置音量  测试期间设置为0
        simpleExoPlayer.volume = 10F
        simpleExoPlayerView?.player = simpleExoPlayer
        //赋值给显示时间
        //赋值给显示时间
        simpleExoPlayer.addListener(object : Player.EventListener {
            override fun onPlayerError(error: ExoPlaybackException?) {
                super.onPlayerError(error)
            }
        })
        simpleExoPlayerView?.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
        simpleExoPlayer.repeatMode = Player.REPEAT_MODE_ALL
        //开启播放
        simpleExoPlayer.playWhenReady = true
    }
}